package com.smart.opcda.topic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smart.opcda.entity.TopicEntity;
import com.smart.opcda.util.Utils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author dengqiao
 * @date 2024/3/19 16:23
 */
@Component
public class TopicManager {
    public Map<String, TopicEntity> topicMap = new ConcurrentHashMap<>();

    @PostConstruct
    private void init(){
        String topicJson = Utils.readFile("topic.json");
        JSONArray topicArray = JSON.parseArray(topicJson);
        if(topicArray!=null&&topicArray.size()>0){
            for(Object object:topicArray){
                TopicEntity topicEntity=new TopicEntity();
                JSONObject jsonObject=(JSONObject)object;
                String topicName=jsonObject.getString("topicName");
                topicEntity.setTopicName(topicName);
                String serverId=jsonObject.getString("serverId");
                topicEntity.setServerId(serverId);
                String accessPath=jsonObject.getString("accessPath");
                topicEntity.setAccessPath(accessPath);
                topicMap.put(topicName,topicEntity);
            }
        }
    }

    public Map<String, TopicEntity> getTopicMap() {
        return topicMap;
    }
}
